package com.shouhuobao.bhi.collect;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.collectplus.express.R;
import com.shouhuobao.bhi.collect.model.CollectParcelPointBean;
import com.shouhuobao.bhi.collectpoint.CollectConstant;
import com.shouhuobao.bhi.collectpoint.ServicePointDetailsActivity;

/**
 * 网点及其包裹
 * 
 * @author wind
 * 
 */
public class CollectParcelPointView extends RelativeLayout {

	private CollectParcelPointBean mCollectParcelPointBean;
	private boolean mIsCashierDesk;
	private Context mContext;
	private ParcelViewListerner mParcelViewListerner;

	public CollectParcelPointView(Context context, CollectParcelPointBean collectParcelPointBean, boolean isCashierDesk, ParcelViewListerner pvl) {
		super(context);
		mCollectParcelPointBean = collectParcelPointBean;
		mIsCashierDesk = isCashierDesk;
		mContext = context;
		mParcelViewListerner = pvl;
		initView(context);
	}

	private View convertView;

	public void initView(Context context) {
		LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		convertView = LayoutInflater.from(context).inflate(R.layout.collect_agent_point_pay_item, null);
		convertView.setLayoutParams(ll);

		RelativeLayout mCollectPointName = (RelativeLayout) convertView.findViewById(R.id.point_name);
		TextView mCollectAgentName = (TextView) convertView.findViewById(R.id.collect_agent_name);
		ListView mParcelListview = (ListView) convertView.findViewById(R.id.parcel_listview);
		TextView mCollectAgentToPay = (TextView) convertView.findViewById(R.id.collect_agent_to_pay);

		this.setLayoutParams(ll);
		this.addView(convertView);
		if (mIsCashierDesk) {
			mCollectAgentToPay.setVisibility(View.GONE);
			convertView.findViewById(R.id.split_line_ll).setVisibility(View.GONE);
		}

		mCollectAgentName.setText("收货宝 " + mCollectParcelPointBean.getId() + " " + mCollectParcelPointBean.getName());
		mCollectPointName.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, ServicePointDetailsActivity.class);
				intent.putExtra(CollectConstant.INTENT_POINT_ID, mCollectParcelPointBean.getId());
				Bundle bundle = new Bundle();
				bundle.putString("point_id", mCollectParcelPointBean.getId() + "");
				intent.putExtras(bundle);
				mContext.startActivity(intent);
			}
		});
		if (mCollectParcelPointBean.getNeedPayMoney().equals("0")) {
			mCollectAgentToPay.setText("查看取货密码");
			mCollectAgentToPay.setTextColor(mContext.getResources().getColor(R.color.white));
			mCollectAgentToPay.setBackgroundResource(R.drawable.collect_button_black);
		} else {
			mCollectAgentToPay.setText("支付此店费用");
			mCollectAgentToPay.setBackgroundResource(R.drawable.collect_button_yellow);
		}

		mCollectAgentToPay.setTag(mCollectParcelPointBean);
		mCollectAgentToPay.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mCollectParcelPointBean.getNeedPayMoney().equals("0")) {
					if (mParcelViewListerner != null) {
						mParcelViewListerner.onClick(mCollectParcelPointBean.getId() + "");
					}
				}
			}
		});
		// 列表
		CollectParcelListAdapter collectParcelListAdapter = new CollectParcelListAdapter(context, mCollectParcelPointBean.getParcels());
		mParcelListview.setAdapter(collectParcelListAdapter);
	}

	public interface ParcelViewListerner {
		void onClick(String pointId);
	}

}
